<template>
  <div>
    <el-dialog
      title="添加背景"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
      >
        <el-form-item label="名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="背景图">
          <el-upload
            class="avatar-uploader"
            :action="imageUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="form.imgOne" :src="form.imgOne" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="handleOk"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<script>
  import {saveOrUpdate} from "@/api/set";
  import common from '@/utils/common'

  export default {
    name: "edit",
    data() {
      return {
        dialogVisible: false,
        imageUrl: common.imgUpUrl,
        form: {
          name: '',
          imgOne: '',
          id: ''

        }
      }
    },
    methods: {

      show(data) {
        this.form.id = data.id
        this.form.name = data.name
        this.form.imgOne = data.imgOne
        this.dialogVisible = true
      },

      //上传成功
      handleAvatarSuccess(file) {
        this.form.imgOne = file.result;
      },
      handleOk() {
        console.log(this.form.id)
        saveOrUpdate(this.form).then(res => {
          if (res.status === 200) {
            this.$message.success("编辑成功")
            this.$emit('Ok')
            this.dialogVisible = false
          }
        })
      }
    }
  }
</script>


<style scoped>
  .avatar {
    width: 178px;
    height: 178px;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    border: 1px dashed #8c939d;
    text-align: center;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
</style>
